﻿using System;
using System.Collections.Generic;

namespace OrderSystem.Core.Domain
{
    public class Order : BaseEntity
    {
        public virtual string Number { get; set; }
        public virtual DateTime CreationDate { get; set; }
        public virtual Customer Customer { get; set; }
        public virtual string Tag { get; set; }

        private IList<OrderItem> orderItems = new List<OrderItem>();
        public virtual IList<OrderItem> OrderItems
        {
            get { return orderItems; }
            set { orderItems = value; }
        }

        public override string ToString()
        {
            return string.Format("Id = {0} Number = {1}", Id, Number);
        }

        public override int GetHashCode()
        {
            return string.Format("{0}{1}{2}{3}{4}", GetType().Name, Number, CreationDate.ToShortDateString(),Customer.GetHashCode(), Tag).GetHashCode();
        }

        public override bool Equals(object obj)
        {
            var compareOrder = obj as Order;

            if (compareOrder == null) return false;

            if (compareOrder.GetHashCode() != GetHashCode()) return false;

            if (compareOrder.OrderItems.Count != OrderItems.Count)return false;

            for (int i = 0; i < OrderItems.Count; i++)
            {
                if (!OrderItems[i].Equals(compareOrder.OrderItems[i]))
                {
                    return false;
                }
            }

            return true;
        }
    }
}